// Wave log module
#include "burner.h"
#include "wave_writer.h"

bool soundLogStart = false;

int waveLogStart()
{
	waveLogStop(); // make sure old log is closed

	wchar_t szFilter[1024];
	wchar_t* pszFilter = szFilter;
	swprintf(pszFilter, localiseStringEx(IDS_DISK_FILE_SOUND), APP_TITLE);
	memcpy(pszFilter + wcslen(pszFilter), L" (*.wav)\0*.wav\0\0", 16 * sizeof(wchar_t));
	if (commonFileDialog(hScrnWnd, true, 0, szFilter, 0, L"wav")) {
		return 1;
	}

	{
		if (!wave_open(audSampleRate, WtoA(commonChoice)))
		{
			FBAPopupAddText(PUF_TEXT_DEFAULT, MAKEINTRESOURCE(IDS_ERR_DISK_CREATE));
			FBAPopupAddText(PUF_TEXT_DEFAULT, MAKEINTRESOURCE(IDS_DISK_SOUND));
			FBAPopupDisplay(PUF_TYPE_ERROR);
			return 1;
		}
		wave_enable_stereo();
		soundLogStart = true;
	}
	menuSync(MENUT_FILE);
	return 0;
}

int waveLogStop()
{
	if (soundLogStart)
	{
		wave_close();
		soundLogStart = false;
		menuSync(MENUT_FILE);
	}
	return 0;
}

void waveLogWrite()
{
	// log to the file
	if (soundLogStart && pBurnSoundOut != NULL) {
		wave_write(pBurnSoundOut, nBurnSoundLen << 1);
		pBurnSoundOut = NULL;
	}
}
